package com.hjq.www.Dao.Impl;

import com.hjq.www.Dao.ZoosDao;
import com.hjq.www.utils.JDBCUtils;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class ZoosDaoImpl implements ZoosDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    private JTextField idField;
    private JTextField nameField;
    private JTextField typeField;
    private JTextField introductionField;
    private JTextField addressField;
    private JTextField areaField;
    private JTextField startTimeField;
    private JTextField endTimeField;
    private JTextField bidField;
    @Override
    public void add(JTextField idField, JTextField nameField, JTextField typeField, JTextField introductionField, JTextField addressField, JTextField areaField, JTextField startTimeField, JTextField endTimeField, JTextField bidField) {
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into zoopark(id,name,type,introduction,address,area,startTime,endTime,bid) value(?,?,?,?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, idField.getText());
            ps.setString(2, nameField.getText());
            ps.setString(3, typeField.getText());
            ps.setString(4, introductionField.getText());
            ps.setString(5, addressField.getText());
            ps.setString(6, areaField.getText());
            ps.setString(7, startTimeField.getText());
            ps.setString(8, endTimeField.getText());
            ps.setString(9, bidField.getText());
            ps.executeUpdate();
            conn.commit();
            JOptionPane.showMessageDialog(null, "添加成功");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "添加失败");
            e.printStackTrace();
        }
    }

    @Override
    public void update(JTextField idField, JTextField nameField, JTextField typeField, JTextField introductionField, JTextField addressField, JTextField areaField, JTextField startTimeField, JTextField endTimeField, JTextField bidField) {
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update zoopark set id=?,name=?,type=?,introduction=?,address=?,startTime=?,endTime=?,bid=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,idField.getText());
            ps.setString(2,nameField.getText());
            ps.setString(3,typeField.getText());
            ps.setString(4,introductionField.getText());
            ps.setString(5,addressField.getText());
            ps.setString(6,areaField.getText());
            ps.setString(7,startTimeField.getText());
            ps.setString(8,endTimeField.getText());
            ps.setString(9,bidField.getText());
            if(ps.executeUpdate()==1){
                JOptionPane.showMessageDialog(null, "修改成功");
            }else{JOptionPane.showMessageDialog(null,"修改失败");}
        }catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "修改失败");
            e.printStackTrace();
        }
    }

    @Override
    public void search(JTextField idField, JTextField nameField, JTextField typeField, JTextField introductionField, JTextField addressField, JTextField areaField, JTextField startTimeField, JTextField endTimeField, JTextField bidField) {
        String sid=idField.getText();
        boolean flag=false;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select * from zoopark where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, sid);
            rs = ps.executeQuery();
            while (rs.next()) {
                String ssid = rs.getString("id");
                if (sid.equals(ssid)) {
                    idField.setText(rs.getString("id"));
                    nameField.setText(rs.getString("name"));
                    typeField.setText(rs.getString("type"));
                    introductionField.setText(rs.getString("introduction"));
                    addressField.setText(rs.getString("address"));
                    areaField.setText(rs.getString("area"));
                    startTimeField.setText(rs.getString("startTime"));
                    endTimeField.setText(rs.getString("endTime"));
                    bidField.setText(rs.getString("bid"));
                    flag=true;
                }
            }
            if(flag){
                JOptionPane.showMessageDialog(null, "查询成功");
            }else{JOptionPane.showMessageDialog(null,"查询失败");}
        }catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "查询失败");
            e.printStackTrace();
        }finally{
            JDBCUtils.close(conn,ps,rs);
        }
    }

    @Override
    public void delete(JTextField idField) {
        String sno = idField.getText();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "delete from zoopark where id =?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            ps.execute();
            JOptionPane.showMessageDialog(null, "删除成功");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"删除失败");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps);
        }
    }
    /**
     * 重置
     */
    @Override
    public void reset(JTextField idField,JTextField nameField,JTextField typeField,JTextField introductionField,JTextField addressField,JTextField areaField,JTextField startTimeField,JTextField endTimeField,JTextField bidField){
        idField.setText("");
        nameField.setText("");
        typeField.setText("");
        introductionField.setText("");
        addressField.setText("");
        areaField.setText("");
        startTimeField.setText("");
        endTimeField.setText("");
        bidField.setText("");
    }
}
